//
//  InspectionDetailCell.swift
//  Employee
//
//  Created by swordjoy on 2017/4/11.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class InspectionDetailCell: UITableViewCell {
  
  static var identifier = "InspectionDetailCell.swordjoy"
  
  static func height(with model: InspectionProject) -> CGFloat {
    
    var calculateY: CGFloat = 0
    for title in model.value { calculateY += (UIFont.calculateHeight(width: UIScreen.width - 130, font: GlobalConfig.system14Font, text: title) + 10) }
    return calculateY + 100
  }
  
  var isFlow: Bool = false {
    didSet {
      if isFlow {
        contentV.snp.updateConstraints({ (make) in
          make.left.top.right.bottom.equalToSuperview()
        })
      }
    }
  }
  
  var isDetail: Bool = false {
    didSet {
      if isDetail {
        titleLabel.textColor = GlobalConfig.TextLightColor
        for label in values {
            label.textColor = GlobalConfig.TextDeepColor
        }
      }
    }
  }
  
  var model: InspectionProject? {
    didSet {
      guard let temp = model else { return }
      titleLabel.text = temp.name
      hotLayoutLabels(with: temp.value)
    }
  }
  
  fileprivate var contentV: UIView!
  
  fileprivate var values: [UILabel] = []
  
  fileprivate var titleLabel: UILabel!
  
  fileprivate var margin: CGFloat = 65

  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    backgroundColor = UIColor.clear
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
}

// MARK: - fileprivate

extension InspectionDetailCell {
  
  fileprivate func create() {
    
    contentV = UIView()
    contentView.addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.top.equalTo(5)
      make.bottom.equalTo(0)
    }
    contentV.layer.cornerRadius = 4
    contentV.clipsToBounds = true
    
    titleLabel = UILabel()
    contentV.addSubview(titleLabel)
    titleLabel.font = GlobalConfig.system16Font
    titleLabel.textColor = GlobalConfig.TextDeepColor
    titleLabel.numberOfLines = 0
    titleLabel.snp.makeConstraints { (make) in
      make.left.top.equalTo(20)
      make.right.equalTo(-20)
    }

  }
  
  fileprivate func hotLayoutLabels(with titles: [String]) {
    
    // 因为重用所以要重新布局
    for view in values { if let _ = view.superview { view.removeFromSuperview() } }
    values.removeAll()
    
    var cacheY: CGFloat = 0
    
    for title in titles {
      let height = UIFont.calculateHeight(width: UIScreen.width - 130, font: GlobalConfig.system14Font, text: title)
      let y = margin + cacheY
      let label = UILabel(frame: CGRect(x: 40, y:  y, width: UIScreen.width - 130, height: height + 10))
      label.font = GlobalConfig.system14Font
      label.textColor = GlobalConfig.BGSkyBlueColor
      label.numberOfLines = 0
      
      if title.isEmpty {
        label.text = "未填写"
        label.textColor = GlobalConfig.TextLightColor
      } else {
        label.text = title
        label.textColor = GlobalConfig.BGSkyBlueColor
      }
    
      cacheY += (height + 10)
      
      values.append(label)
      contentV.addSubview(label)
    }
    
  }
  
}
